home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 2 / Meeting Pearls Vol. II (1995)(GTI - Schatztruhe)[!].iso / Pearls / tex / PasTeX14-BETA-6 / SpecialHost / Source / Tools.c < prev    next >
C/C++ Source or Header  |  1994-08-03  |  12KB  |  553 lines

  1. /*
  2. **    SpecialHost for PasTeX
  3. **
  4. **    Copyright © by Olaf Barthel & Georg Heßmann
  5. */
  6.  
  7. #include "Global.h"
  8.  
  9. STATIC int UseFastMem = 0;    /* (hes) some kind of test: does SpecialHost work with Bitmap in FAST-RAM too?    */
  10.                 /* No! There are used some OS functions, which need CHIP-RAM!            */
  11.  
  12.  
  13. VOID __regargs
  14. FreeVecPooled(APTR Mem)
  15. {
  16.     if(Mem)
  17.     {
  18.         ULONG *Data = Mem;
  19.  
  20.         LibFreePooled(Pool,&Data[-1],Data[-1]);
  21.     }
  22. }
  23.  
  24. APTR __regargs
  25. AllocVecPooled(LONG Size,ULONG Flags)
  26. {
  27.     if(Size)
  28.     {
  29.         ULONG *Data;
  30.  
  31.         Size += sizeof(ULONG);
  32.  
  33.         if(Data = (ULONG *)LibAllocPooled(Pool,Size))
  34.         {
  35.             *Data++ = Size;
  36.  
  37.             if(Flags & MEMF_CLEAR)
  38.                 memset(Data,0,Size - sizeof(ULONG));
  39.  
  40.             return((APTR)Data);
  41.         }
  42.     }
  43.  
  44.     return(NULL);
  45. }
  46.  
  47. VOID __regargs
  48. ClearList(struct List *List)
  49. {
  50.     struct Node    *Node,
  51.             *Next;
  52.  
  53.     Node = List -> lh_Head;
  54.  
  55.     while(Next = Node -> ln_Succ)
  56.     {
  57.         FreeVecPooled(Node);
  58.  
  59.         Node = Next;
  60.     }
  61.  
  62.     NewList(List);
  63. }
  64.  
  65. struct ListEntry * __regargs
  66. NewEntry(STRPTR Title)
  67. {
  68.     struct ListEntry *Entry;
  69.  
  70.     if(Entry = (struct ListEntry *)AllocVecPooled(sizeof(struct ListEntry) + strlen(Title),MEMF_ANY))
  71.     {
  72.         Entry -> Title = (STRPTR)(Entry + 1);
  73.  
  74.         strcpy(Entry -> Title,Title);
  75.     }
  76.  
  77.     return(Entry);
  78. }
  79.  
  80. WORD __stdargs
  81. ShowRequest(struct Window *Window,STRPTR Text,STRPTR Gadgets,...)
  82. {
  83.     WORD Result;
  84.  
  85.     if(AP_Application)
  86.     {
  87.         va_list VarArgs;
  88.  
  89.         va_start(VarArgs,Gadgets);
  90.         Result = MUI_RequestA(AP_Application,WI_Main,NULL,"SpecialHost",Gadgets,Text,VarArgs);
  91.         va_end(VarArgs);
  92.     }
  93.     else
  94.     {
  95.         struct EasyStruct    Easy;
  96.         ULONG            IDCMP = NULL;
  97.         va_list             VarArgs;
  98.  
  99.         Easy . es_StructSize    = sizeof(struct EasyStruct);
  100.         Easy . es_Flags        = NULL;
  101.         Easy . es_Title        = "SpecialHost";
  102.         Easy . es_TextFormat    = Text;
  103.         Easy . es_GadgetFormat    = Gadgets;
  104.  
  105.         va_start(VarArgs,Gadgets);
  106.         Result = EasyRequestArgs(Window,&Easy,&IDCMP,VarArgs);
  107.         va_end(VarArgs);
  108.     }
  109.  
  110.     return(Result);
  111. }
  112.  
  113. STRPTR __regargs
  114. ShowError(LONG Primary,LONG Secondary,BOOL GetPrimary)
  115. {
  116.     STATIC struct { LONG Code; STRPTR Name; } LocalErrors[] =
  117.     {
  118.         ERR_NO_INTUITION,        "Error opening intuition.library v37",
  119.         ERR_NO_GRAPHICS,        "Error opening graphics.library v37",
  120.         ERR_NO_GADTOOLS,        "Error opening gadtools.library v37",
  121.         ERR_NO_ICON,            "Error opening icon.library v37",
  122.         ERR_NO_IFFPARSE,        "Error opening iffparse.library v37",
  123.         ERR_NO_UTILITY,            "Error opening utility.library v37",
  124.         ERR_NO_ASL,            "Error opening asl.library v37",
  125.         ERR_NO_MUI,            "Error opening muimaster.library",
  126.         ERR_NO_MATHFFP,            "Error opening mathieeedoubbas.library",
  127.         ERR_NO_MATHTRANS,        "Error opening mathieeedoubtrans.library",
  128.         ERR_NO_POOL,            "Error creating memory pool",
  129.         ERR_NO_GUI,            "Error opening window",
  130.         ERR_ALREADY_RUNNING,        "SpecialHost process already running",
  131.         ERR_NO_PORT,            "Error opening message port",
  132.         ERR_READ_ERROR,            "Error reading file",
  133.         ERR_NO_MEM,            "Out of memory",
  134.         ERR_FILE_FORMAT_ERROR,        "File format corrupt",
  135.         ERR_WEIRD_COMPRESSION,        "Image compression type not supported",
  136.         ERR_WRONG_IMAGE_FORMAT,        "Cannot handle picture file format",
  137.         ERR_TOO_LARGE,            "Requested image size is too large",
  138.         ERR_TOO_SMALL,            "Requested image size is too small",
  139.         ERR_NO_NIL,            "Error opening NIL: stream",
  140.         ERR_NO_POST,            "Error opening post.library v15",
  141.  
  142.         DTERROR_UNKNOWN_DATATYPE,    "Unknown file format",
  143.         DTERROR_COULDNT_SAVE,        "Could not store file",
  144.         DTERROR_COULDNT_OPEN,        "Could not open file",
  145.         DTERROR_COULDNT_SEND_MESSAGE,    "Internal send message error",
  146.         DTERROR_COULDNT_OPEN_CLIPBOARD,    "Could not open clipboard",
  147.         DTERROR_UNKNOWN_COMPRESSION,    "Compression method unknown",
  148.         DTERROR_NOT_ENOUGH_DATA,    "Not enough data available",
  149.         DTERROR_INVALID_DATA,        "Invalid data",
  150.  
  151.         19999,                "Postscript error: File not found",
  152.         20001,                "Postscript error: Dictionary full",
  153.         20002,                "Postscript error: Dictionary stack overflow",
  154.         20003,                "Postscript error: Dictionary stack underflow",
  155.         20004,                "Postscript error: Exec stack overflow",
  156.         20005,                "Postscript error: Interrupted",
  157.         20006,                "Postscript error: Invalid access",
  158.         20007,                "Postscript error: Invalid exit",
  159.         20008,                "Postscript error: Invalid file access",
  160.         20009,                "Postscript error: Invalid font",
  161.         20010,                "Postscript error: Invalid restore",
  162.         20011,                "Postscript error: Invalid stop",
  163.         20012,                "Postscript error: I/O error",
  164.         20013,                "Postscript error: Limit check trap",
  165.         20014,                "Postscript error: No current point",
  166.         20015,                "Postscript error: Range check trap",
  167.         20016,                "Postscript error: Stack overflow",
  168.         20017,                "Postscript error: Stack underflow",
  169.         20018,                "Postscript error: Syntax error",
  170.         20019,                "Postscript error: Timeout",
  171.         20020,                "Postscript error: Typecheck",
  172.         20021,                "Postscript error: Undefined",
  173.         20022,                "Postscript error: Undefined filename",
  174.         20023,                "Postscript error: Undefined result",
  175.         20024,                "Postscript error: Unmatched mark",
  176.         20025,                "Postscript error: Unregistered",
  177.         20026,                "Postscript error: Virtual memory error",
  178.         20027,                "Postscript error: Memory allocation error",
  179.         20028,                "Postscript error: Killed",
  180.         20029,                "Postscript error: Configuration",
  181.         20030,                "Postscript error: Undefined resource",
  182.  
  183.         IFFERR_NOMEM,            "Out of memory",
  184.         IFFERR_READ,            "File read error",
  185.         IFFERR_WRITE,            "File write error",
  186.         IFFERR_SEEK,            "File seek error",
  187.         IFFERR_MANGLED,            "File structure damaged",
  188.         IFFERR_NOTIFF,            "Not an IFF format file",
  189.  
  190.         0,                NULL
  191.     };
  192.  
  193.     STRPTR    PrimaryError    = NULL,
  194.         SecondaryError    = NULL;
  195.  
  196.     if(Primary)
  197.     {
  198.         LONG i;
  199.  
  200.         for(i = 0 ; LocalErrors[i] . Name ; i++)
  201.         {
  202.             if(LocalErrors[i] . Code == Primary)
  203.             {
  204.                 PrimaryError = LocalErrors[i] . Name;
  205.  
  206.                 break;
  207.             }
  208.         }
  209.  
  210.         if(!PrimaryError)
  211.         {
  212.             STATIC UBYTE __far Buffer[256];
  213.  
  214.             Fault(Primary,"",Buffer,256);
  215.  
  216.             PrimaryError = Buffer + 2;
  217.         }
  218.  
  219.         if(GetPrimary)
  220.             return(PrimaryError);
  221.     }
  222.  
  223.     if(Secondary)
  224.     {
  225.         LONG i;
  226.  
  227.         for(i = 0 ; LocalErrors[i] . Name ; i++)
  228.         {
  229.             if(LocalErrors[i] . Code == Secondary)
  230.             {
  231.                 SecondaryError = LocalErrors[i] . Name;
  232.  
  233.                 break;
  234.             }
  235.         }
  236.  
  237.         if(!SecondaryError)
  238.         {
  239.             STATIC UBYTE __far Buffer[256];
  240.  
  241.             Fault(Secondary,"",Buffer,256);
  242.  
  243.             SecondaryError = Buffer + 2;
  244.         }
  245.     }
  246.  
  247.     if(PrimaryError)
  248.     {
  249.         if(ThisProcess -> pr_CLI)
  250.         {
  251.             if(SecondaryError)
  252.                 Printf("SpecialHost: %s, %s\a\n",PrimaryError,SecondaryError);
  253.             else
  254.                 Printf("SpecialHost: %s\a\n",PrimaryError);
  255.         }
  256.         else
  257.         {
  258.             if(IntuitionBase)
  259.             {
  260.                 if(SecondaryError)
  261.                     ShowRequest(NULL,"%s\n%s","Continue",PrimaryError,SecondaryError);
  262.                 else
  263.                     ShowRequest(NULL,"%s","Continue",PrimaryError);
  264.             }
  265.         }
  266.     }
  267. }
  268.  
  269. VOID __regargs
  270. DeleteBitMap(struct BitMap *BitMap)
  271. {
  272.     if(GfxBase -> LibNode . lib_Version >= 39 && !UseFastMem)
  273.         FreeBitMap(BitMap);
  274.     else
  275.     {
  276.         LONG i;
  277.  
  278.         for(i = 0 ; i < BitMap -> Depth ; i++)
  279.         {
  280.             if(BitMap -> Planes[i])
  281.                 FreeVec(BitMap -> Planes[i]);
  282.         }
  283.  
  284.         FreeVecPooled(BitMap);
  285.     }
  286. }
  287.  
  288. struct BitMap * __regargs
  289. CreateBitMap(LONG Width,LONG Height,LONG Depth,ULONG Flags,struct BitMap *Friend)
  290. {
  291.     if(GfxBase -> LibNode . lib_Version < 39 || UseFastMem)
  292.     {
  293.         struct BitMap    *BitMap;
  294.         LONG         Plus;
  295.  
  296.         if(Depth > 8)
  297.             Plus = (Depth - 8) * sizeof(PLANEPTR);
  298.         else
  299.             Plus = 0;
  300.  
  301.         if(BitMap = (struct BitMap *)AllocVecPooled(sizeof(struct BitMap) + Plus,MEMF_ANY | MEMF_CLEAR))
  302.         {
  303.             LONG i,PageSize;
  304.  
  305.             InitBitMap(BitMap,Depth,Width,Height);
  306.  
  307.             PageSize = BitMap -> BytesPerRow * BitMap -> Rows;
  308.  
  309.             for(i = 0 ; i < BitMap -> Depth ; i++)
  310.             {
  311.                 if (UseFastMem)
  312.                 {
  313.                     BitMap -> Planes[i] = (PLANEPTR)AllocVec(PageSize,MEMF_ANY | ((Flags & BMF_CLEAR) ? MEMF_CLEAR : 0));
  314.                 }
  315.                 else
  316.                 {
  317.                     BitMap -> Planes[i] = (PLANEPTR)AllocVec(PageSize,MEMF_CHIP);
  318.                 }
  319.  
  320.                 if(!BitMap -> Planes[i])
  321.                 {
  322.                     LONG j;
  323.  
  324.                     for(j = 0 ; j < i ; j++)
  325.                         FreeVec(BitMap -> Planes[j]);
  326.  
  327.                     FreeVecPooled(BitMap);
  328.  
  329.                     return(NULL);
  330.                 }
  331.             }
  332.  
  333.             if((Flags & BMF_CLEAR) && !UseFastMem)
  334.                 BltBitMap(BitMap,0,0,BitMap,0,0,Width,Height,0x00,(1 << Depth) - 1,NULL);
  335.  
  336.             return(BitMap);
  337.         }
  338.     }
  339.     else
  340.         return(AllocBitMap(Width,Height,Depth,Flags,Friend));
  341. }
  342.  
  343. VOID __regargs
  344. DeleteTempLine(UBYTE *Line)
  345. {
  346.     FreeVecPooled(Line);
  347. }
  348.  
  349. UBYTE * __regargs
  350. CreateTempLine(LONG Width,LONG Height)
  351. {
  352.     return((UBYTE *)AllocVecPooled(((Width + 15) & ~15) * Height,MEMF_ANY));
  353. }
  354.  
  355. VOID __regargs
  356. DeleteTempRPort(struct RastPort *Temp)
  357. {
  358.     DeleteBitMap(Temp -> BitMap);
  359.  
  360.     FreeVecPooled(Temp);
  361. }
  362.  
  363. struct RastPort * __regargs
  364. CreateTempRPort(struct RastPort *Source)
  365. {
  366.     struct RastPort *Temp;
  367.  
  368.     if(Temp = (struct RastPort *)AllocVecPooled(sizeof(struct RastPort),MEMF_ANY))
  369.     {
  370.         LONG Width,Depth;
  371.  
  372.         CopyMem(Source,Temp,sizeof(struct RastPort));
  373.  
  374.         Temp -> Layer = NULL;
  375.  
  376.         if(GfxBase -> LibNode . lib_Version < 39)
  377.         {
  378.             Width    = Source -> BitMap -> BytesPerRow * 8;
  379.             Depth    = Source -> BitMap -> Depth;
  380.         }
  381.         else
  382.         {
  383.             Width    = GetBitMapAttr(Source -> BitMap,BMA_WIDTH);
  384.             Depth    = GetBitMapAttr(Source -> BitMap,BMA_DEPTH);
  385.         }
  386.  
  387.         if(Temp -> BitMap = CreateBitMap(Width,1,Depth,NULL,Source -> BitMap))
  388.             return(Temp);
  389.         else
  390.             FreeVecPooled(Temp);
  391.     }
  392.  
  393.     return(NULL);
  394. }
  395.  
  396. LONG __regargs
  397. FileDateCheck(STRPTR File1,STRPTR File2,LONG *Error)
  398. {
  399.     struct FileInfoBlock    *FileInfo;
  400.     LONG             Result = 0;
  401.  
  402.     *Error = 0;
  403.  
  404.     if(FileInfo = (struct FileInfoBlock *)AllocDosObjectTags(DOS_FIB,TAG_DONE))
  405.     {
  406.         BPTR FileLock;
  407.  
  408.         if(FileLock = Lock(File1,ACCESS_READ))
  409.         {
  410.             if(Examine(FileLock,FileInfo))
  411.             {
  412.                 struct DateStamp Date1;
  413.  
  414.                 Date1 = FileInfo -> fib_Date;
  415.  
  416.                 UnLock(FileLock);
  417.  
  418.                 if(FileLock = Lock(File2,ACCESS_READ))
  419.                 {
  420.                     if(Examine(FileLock,FileInfo))
  421.                     {
  422.                         struct DateStamp Date2;
  423.  
  424.                         Date2 = FileInfo -> fib_Date;
  425.  
  426.                         Result = CompareDates(&Date1,&Date2);
  427.                     }
  428.                     else
  429.                         *Error = IoErr();
  430.                 }
  431.                 else
  432.                     *Error = IoErr();
  433.             }
  434.             else
  435.                 *Error = IoErr();
  436.  
  437.             if(FileLock)
  438.                 UnLock(FileLock);
  439.         }
  440.         else
  441.             *Error = IoErr();
  442.  
  443.         FreeDosObject(DOS_FIB,FileInfo);
  444.     }
  445.     else
  446.         *Error = ERR_NO_MEM;
  447.  
  448.     return(Result);
  449. }
  450.  
  451. VOID __regargs
  452. AddProtection(STRPTR Name,ULONG Mask)
  453. {
  454.     struct FileInfoBlock *FileInfo;
  455.  
  456.     if(FileInfo = (struct FileInfoBlock *)AllocDosObject(DOS_FIB,TAG_DONE))
  457.     {
  458.         BPTR FileLock;
  459.  
  460.         if(FileLock = Lock(Name,ACCESS_READ))
  461.         {
  462.             if(Examine(FileLock,FileInfo))
  463.             {
  464.                 UnLock(FileLock);
  465.  
  466.                 SetProtection(Name,FileInfo -> fib_Protection | Mask);
  467.             }
  468.             else
  469.                 UnLock(FileLock);
  470.         }
  471.  
  472.         FreeDosObject(DOS_FIB,FileInfo);
  473.     }
  474. }
  475.  
  476. STATIC LONG    MaxProgress    = 1,
  477.         LastProgress    = -1;
  478.  
  479. VOID __regargs
  480. SetMaxProgress(LONG Count)
  481. {
  482.     if(GA_Gauge)
  483.     {
  484.         SetAttrs(GA_Gauge,
  485.             MUIA_Gauge_Current,    0,
  486.             MUIA_Gauge_Max,        Count,
  487.         TAG_DONE);
  488.     }
  489.     else
  490.     {
  491.         if(LastProgress != 100 && LastProgress != -1)
  492.             FPrintf(ThisProcess -> pr_COS,"\n");
  493.  
  494.         FPrintf(ThisProcess -> pr_COS,"\33[0 pWorking:   0%");
  495.         Flush(ThisProcess -> pr_COS);
  496.  
  497.         MaxProgress    = Count;
  498.         LastProgress    = 0;
  499.     }
  500. }
  501.  
  502. VOID __regargs
  503. ShowProgress(LONG Count)
  504. {
  505.     if(GA_Gauge)
  506.         set(GA_Gauge,MUIA_Gauge_Current,Count);
  507.     else
  508.     {
  509.         if(ThisProcess -> pr_CLI)
  510.         {
  511.             Count = (100 * Count) / MaxProgress;
  512.  
  513.             if(Count < 0)
  514.                 Count = 0;
  515.             else
  516.             {
  517.                 if(Count > 100)
  518.                     Count = 100;
  519.             }
  520.  
  521.             if(LastProgress != Count)
  522.             {
  523.                 FPrintf(ThisProcess -> pr_COS,"\33[4D%3ld%%",Count);
  524.  
  525.                 Flush(ThisProcess -> pr_COS);
  526.  
  527.                 LastProgress = Count;
  528.  
  529.                 if(LastProgress == 100)
  530.                 {
  531.                     FPrintf(ThisProcess -> pr_COS,"\n\33[ p");
  532.  
  533.                     Flush(ThisProcess -> pr_COS);
  534.                 }
  535.             }
  536.         }
  537.     }
  538. }
  539.  
  540. LONG __regargs
  541. GetMapCode(struct MapTable *Table,STRPTR Key)
  542. {
  543.     while(Table -> Key)
  544.     {
  545.         if(!Stricmp(Table -> Key,Key))
  546.             return(Table -> ID);
  547.         else
  548.             Table++;
  549.     }
  550.  
  551.     return(-1);
  552. }
  553.